package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {
	getAPIToken()
	targetUrl := "http://localhost:8080/api/v3/requests/service_category?"
	payload := strings.NewReader(`input_data={"list_info":{"row_count":"100","start_index":1,"filter_by":{"name":"hasIncidentTemplates"},"fields_required":["id","name","description","image","sort_index","templates"]}}`)

	req, _ := http.NewRequest("GET", targetUrl, payload)

	req.Header.Add("authtoken", "E54A5361-2EA6-4805-97CE-5C1718DB08A8")
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("err: ", err)
	}
	defer response.Body.Close()
	fmt.Println("response :", response.StatusCode)
	body, _ := ioutil.ReadAll(response.Body)
	fmt.Println(string(body))
}

func getAPIToken() {
	targetUrl := "http://localhost:8080/APIKeyGeneration.do?"
	payload := strings.NewReader(`module=generateAPIKeyFromAdmin&domainName=null&loginName=dss&sdpcsrfparam=da9d3a7c37e59bdda8f85cb8ccb87bb130cb82ea41bcc0e7627a997a847dbd88f1a2a842c14f6c13e156fd0344e01c11b265206f6cac9d1282a2aff98b731cf1`)

	req, _ := http.NewRequest("GET", targetUrl, payload)
    
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
	req.Header.Add("AUTHTOKEN", "E54A5361-2EA6-4805-97CE-5C1718DB08A8")
	// req.Header.Add("Referer", "http://localhost:8080/SetUpWizard.do?forwardTo=technician&isUser=false&viewType=edit&userId=6")
	// req.Header.Add("X-ZCSRF-TOKEN", "sdpcsrfparam=da9d3a7c37e59bdda8f85cb8ccb87bb130cb82ea41bcc0e7627a997a847dbd88f1a2a842c14f6c13e156fd0344e01c11b265206f6cac9d1282a2aff98b731cf1")
sql := `SELECT ant.title AS title, ant.message AS body, ant.ispublic AS ispublic, anc.name AS an_type, p.priorityname AS priority, 
	ant.date_from AS start_time, ant.date_to AS end_time, u.first_name AS create_by, ant.createddate as create_time from announcement ant 
LEFT JOIN announcementtype anc ON anc.announcementtypeid = ant.announcementtypeid 
LEFT JOIN aaauser u ON u.user_id = ant.userid
LEFT JOIN prioritydefinition p ON p.priorityid = ant.priorityid`
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("err: ", err,sql)
	}
	defer response.Body.Close()
	fmt.Println("get api --------------response :", response)
	body, _ := ioutil.ReadAll(response.Body)
	fmt.Println("get body ------ ", string(body))
}

func TicketGet() {

}

func TicketCreate() {

type SessionField struct {
	Id            int64  `json:"id,omitempty" gorm:"column:id"`
	JsEssionidSso string `json:"js_essionid_sso" gorm:"column:js_essionid_sso"`
	EncodeField   string `json:"encode_field" gorm:"column:encode_field"`
	SsoId         []byte `json:"sso_id" gorm:"column:sso_id;"`
	LinkSessionID int64  `json:"link_session_id" gorm:"column:link_session_id;"`
}

type AaaaccsessionInfo struct {
	SessionId       int64  `json:"session_id,omitempty"`
	AccountId       int64  `json:"account_id,omitempty"`
	UserHost        string `json:"user_host,omitempty"`
	ApplicationHost string `json:"application_host,omitempty"`
	Opentime        int64  `json:"opentime,omitempty"`
	Closetime       int64  `json:"closetime,omitempty"`
	Status          string `json:"status,omitempty"`
	UserHostName    string `json:"user_host_name,omitempty"`
	Authenticator   string `json:"authenticator,omitempty"`
}

// 如果找到一个 session，
func GetSession() string {

	session_data := []SessionField{}
	fmt.Println("session_data ", session_data)
	// get session id not null
	session_list := db.Table("achttpsession").Joins("left join aaaacchttpsession on aaaacchttpsession.session_id = achttpsession.session_id").Scan(&session_data)
	//
	if len(session_list) > 0 {
		return session_list[0].JsEssionidSso
	} else {
		last_session := SessionField{}
		// unlinked session
		db.Table("achttpsession").Where("link_session_id = 0 || link_session_id = null").First(&last_session)

		var last_session int64

		db.Table("aaaaccsession").Last(&last_session)
		session_info := AaaaccsessionInfo{
			SessionId:       last_session.SessionId + 1,
			AccountId:       2,
			UserHost:        "0:0:0:0:0:0:0:1",
			ApplicationHost: "localhost",
			Opentime:        time.Now().UnixMicro(),
			Closetime:       0,
			Status:          "ACTIVE",
			UserHostName:    "0:0:0:0:0:0:0:1",
			Authenticator:   "RememberMeLoginModule",
		}

		db.Table("aaaaccsession").Create(&session_info)
		// update session link data
		db.Model(&last_session).Updates("session_id")
		return last_session.JsEssionidSso
	}
}

}

func 
